package com.xiaoyu.studyworkspace.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.xiaoyu.studyworkspace.common.util.Result;
import com.xiaoyu.studyworkspace.dto.AutoConfigDTO;
import com.xiaoyu.studyworkspace.dto.AutoUserDTO;
import com.xiaoyu.studyworkspace.dto.ChangRoomDTO;
import com.xiaoyu.studyworkspace.dto.SecretDTO;
import com.xiaoyu.studyworkspace.pojo.AutoConfig;
import com.xiaoyu.studyworkspace.pojo.AutoLog;
import com.xiaoyu.studyworkspace.pojo.AutoUser;
import com.xiaoyu.studyworkspace.service.AutoPlayService;
import com.xiaoyu.studyworkspace.vo.AutoUserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/autoplay")
@Slf4j
@CrossOrigin("*")
public class AutoPlayController {

    @Autowired
    private AutoPlayService autoPlayService;


    /**
     * 登录挂机
     *
     * @return
     */
    @PostMapping("login")
    public Result userLoginAutoPlay(@RequestBody SecretDTO secretDTO) throws Exception {
        AutoUser autoUser = autoPlayService.userLoginAutoPlay(secretDTO);
        AutoUserVO autoUserVO = new AutoUserVO();
        if (autoUser == null) {
            Result<String> error = Result.error("手机或者密码错误");
            return error;
        }
        autoPlayService.insertDefaultAutoConfig(autoUser);
        BeanUtils.copyProperties(autoUser, autoUserVO);
        Result<AutoUserVO> success = Result.success(autoUserVO, "登录成功！");
        return success;
    }


    /**
     * 操作挂机
     *
     * @return
     */
    @PostMapping("/start")
    public Result startAuto(@RequestBody AutoUserDTO AutoUserDTO) {
        autoPlayService.startAutoBetting(AutoUserDTO);
        return Result.success(null, "操作成功！");
    }

    /**
     * 用户修改配置表
     *
     * @param autoConfigDTO
     * @return
     */
    @PutMapping("/autoconfig")
    public Result updateAutoConfig(@RequestBody AutoConfigDTO autoConfigDTO) {
        if (autoConfigDTO.getUser_id() == null) {
            return Result.error("挂机的ID必须和开通的ID一致！");
        }
        autoPlayService.updateAutoConfig(autoConfigDTO);
        return Result.success(null, "修改成功！");
    }

    /**
     * 查询用对应的配置
     *
     * @param autoUserDTO
     * @return
     */
    @GetMapping("/autoconfig")
    public Result getAutoConfigByID(AutoUserDTO autoUserDTO) {
        AutoConfig autoConfig = autoPlayService.getAutoConfigByID(autoUserDTO.getUser_id());
        if (autoConfig == null) {
            Result<String> error = Result.error("不存在配置，请配置信息!");
            return error;
        }
        Result<AutoConfig> success = Result.success(autoConfig, "查询配置成功！");
        return success;
    }

    /**
     * 查询用户对应的日志
     *
     * @param autoUserDTO
     * @return
     */
    @GetMapping("/autologs")
    public Result getAutoLogs(AutoUserDTO autoUserDTO) {
        List<AutoLog> autoLogs = autoPlayService.getAutoLogsById(autoUserDTO);
        if (autoLogs == null) {
            Result<String> error = Result.error(null);
            return error;
        }
        Result<List<AutoLog>> success = Result.success(autoLogs, null);
        return success;
    }

    /**
     * 调房
     *
     * @param changRoomDTO
     * @return
     */
    @PostMapping("/chang")
    public Result changRoom(@RequestBody ChangRoomDTO changRoomDTO) throws JsonProcessingException {
        String msg = autoPlayService.changRoom(changRoomDTO);
        return Result.success(null, msg);
    }

}
